<?php

namespace app\admin\controller\pifa;

use app\common\controller\Backend;
use app\admin\library\Auth;
use Exception;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use think\Db;
use think\db\exception\BindParamException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\response\Json;
/**
 * 分类管理
 *
 * @icon fa fa-circle-o
 */
class Caiwu extends Backend
{

    /**
     * Goods模型对象
     * @var \app\admin\model\pifa\Goods
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
       
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        $result['user_num'] = Db::table('fa_user')->count();


        $result['user_pay'] = Db::table('fa_pifa_order')->where([
            'shippingstate' => 0,
            'paystate' => 0
        ])->group('user_id')->count();
        $result['user_nopay'] = $result['user_num'] - $result['user_pay'];
        $result['user_zhanbi'] = !empty($result['user_num']) ? bcdiv($result['user_pay'], $result['user_num'],2) : 0;
        $result['user_zhanbi'] = bcmul($result['user_zhanbi'], 100);



        $result['goods_num'] = Db::table('fa_litestore_goods')->count();


        $result['goods_pay'] = Db::table('fa_pifa_orderitem')->where([
            'status' => 1
        ])->group('goods_id')->count();

        // var_dump($result['goods_pay']);

        $result['goods_nopay'] = $result['goods_num'] - $result['goods_pay'];
        $result['goods_zhanbi'] =  !empty($result['goods_num']) ? bcdiv($result['goods_pay'], $result['goods_num'],2) : 0;
        $result['goods_zhanbi'] = bcmul($result['goods_zhanbi'], 100);


       


        $result['order'] = Db::table('fa_pifa_order')->where([
            'shippingstate' => 0,
            'paystate' => 1
        ])->field('count(*) as num,sum(price) as price,sum(yunfei) as yunfei,sum(tui_price) as tui_price')->find();


        $result['order']['order_avg_price'] = !empty($result['order']['num']) ? bcdiv($result['order']['price'], $result['order']['num'],2) : 0;
        $result['order']['order_avguser_price'] = !empty($result['user_pay']) ?  bcdiv($result['order']['price'], $result['user_pay'],2) : 0;


     
        $this->view->assign("result", $result);
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function index2()
    {
       
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function index3()
    {   
         //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user','inv','mendian','address'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->getRelation('user')->visible(['nickname','mobile','avatar']);
                $row->getRelation('inv')->visible(['nickname','mobile','avatar']);
            }
            
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
     
    }



}
